<?php

namespace Hilaoyu\LaravelExtend\Services\Realnames\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;

class InitDbTableCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'laravelExtend:realnameInitDb {conn?}';
    protected $max_retry = 5;
    protected $retry_step = 60;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'laravel extend realnames init record table';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $conn_name = $this->argument('conn');
        $Db = DB::connection($conn_name);
        $Schema = Schema::connection($conn_name);//$Db->getSchemaBuilder();

        if (!$Schema->hasTable('realnames')) {
            $Schema->create('realnames', function (Blueprint $table) {
                $table->char('id', 36)->primary();
                $table->char('app_id', 36)->nullable()->index()->comment('应用ID');
                $table->char('user_id', 36)->index()->comment('用户ID');
                $table->string('name', 60)->default('')->comment('姓名');
                $table->string('card_type', 60)->index()->default('')->comment('证件类型');
                $table->string('card_no', 60)->index()->default('')->comment('证件号码');
                $table->json('card_img')->nullable()->comment('证件照片');
                $table->json('user_img')->nullable()->comment('用户照片');
                $table->tinyInteger('status')->index()->default(0)->comment('状态');
                $table->dateTime('check_time')->nullable()->comment('通过时间');
                $table->dateTime('remark')->nullable()->comment('审核说明');


                $table->dateTime('created_at')->nullable()->index()->comment('创建时间');
                $table->dateTime('updated_at')->nullable()->index()->comment('最后修改资料时间');
                $table->dateTime('deleted_at')->nullable()->comment('删除时间');
            });

            $Db->statement("ALTER TABLE `" . $Db->getTablePrefix() . "realnames` comment '实名认证记录表'");
        }
        if (config('realname.provide_service', '')) {
            if (!$Schema->hasTable('realname_apps')) {
                $Schema->create('realname_apps', function (Blueprint $table) {
                    $table->char('id', 36)->primary();
                    $table->string('name')->default('')->comment('名称');
                    $table->string('secret', 64)->comment('密钥');
                    $table->json('domain')->comment('域名');
                    $table->string('introduction')->nullable()->comment('介绍');
                    $table->string('logo')->nullable()->comment('LOGO');
                    $table->tinyInteger('status')->default(0)->index()->comment('状态');
                    $table->dateTime('created_at')->nullable()->index()->comment('创建时间');
                    $table->dateTime('updated_at')->nullable()->index()->comment('最后修改资料时间');
                    $table->dateTime('deleted_at')->nullable()->comment('删除时间');
                });

                $Db->statement("ALTER TABLE `" . $Db->getTablePrefix() . "realname_apps` comment '实名应用授权表'");
            }
        }

        return 0;

    }
}
